package com.roamer.audition.server.entity;

import com.roamer.audition.common.enums.ResultEnum;
import com.roamer.audition.common.enums.ScoreEnum;
import com.roamer.audition.common.enums.StatusEnum;
import com.roamer.audition.server.entity.base.LogicDeleteBaseEntity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.Where;

import javax.persistence.*;

/**
 * 面试计划 Entity
 *
 * @author roamer
 * @version V1.0
 * @date 2019-08-20 22:34
 */
@Entity
@Table(name = "t_record", indexes = {@Index(name = "company_id_index", columnList = "company_id"),
        @Index(name = "begin_time_index", columnList = "begin_time"),
        @Index(name = "end_time_index", columnList = "end_time"),})
@Where(clause = "is_deleted = false")
@NamedEntityGraphs(
        {@NamedEntityGraph(name = "auditionRecord.all", attributeNodes = {@NamedAttributeNode(value = "company")})})

@Getter
@Setter
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
public class AuditionRecord extends LogicDeleteBaseEntity {

    private static final long serialVersionUID = -4793139210226287448L;

    /**
     * 所属公司
     */
    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "company_id", nullable = false,
            foreignKey = @ForeignKey(name = "none", value = ConstraintMode.NO_CONSTRAINT,
                    foreignKeyDefinition = "bigint(20) index comment '公司ID'"))
    private Company company;

    /**
     * 开始日期
     */
    @Column(name = "begin_time", length = 20, nullable = false, columnDefinition = "bigint(20) comment '开始日期'")
    private Long beginTime;

    /**
     * 结束时间
     */
    @Column(name = "end_time", length = 20, columnDefinition = "bigint(20) comment '结束日期'")
    private Long endTime;

    /**
     * 备注
     */
    @Column(name = "remark", columnDefinition = "varchar(255) comment '备注'")
    private String remark;

    /**
     * 面试评分
     */
    @Column(name = "score", columnDefinition = "tinyint(1) comment '面试评分'")
    @Type(type = "com.roamer.audition.server.entity.common.DbEnumType")
    private ScoreEnum score;

    /**
     * 进行状态
     */
    @Column(name = "status", nullable = false, columnDefinition = "tinyint(1) default 0 comment '进行状态'")
    @Type(type = "com.roamer.audition.server.entity.common.DbEnumType")
    private StatusEnum status;

    /**
     * 面试结果
     */
    @Column(name = "result", columnDefinition = "tinyint(1) comment '面试结果'")
    @Type(type = "com.roamer.audition.server.entity.common.DbEnumType")
    private ResultEnum result;


    @Override
    @PrePersist
    public void prePersist() {
        super.prePersist();
        if (status == null) {
            status = StatusEnum.UN_STARTED;
        }
    }

}
